/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.ConsumeProduct;
import com.sasis.dao.ConsumeProductDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class ConsumeProductDaoiBatis extends BaseDaoiBATIS implements ConsumeProductDao {

    /**
     * @see com.sasis.dao.ConsumeProductDao#getConsumeProducts(com.sasis.model.ConsumeProduct)
     */
    public List getConsumeProducts(final ConsumeProduct consumeProduct) {
          return getSqlMapClientTemplate().queryForList("getConsumeProducts", consumeProduct);
    }

    /**
     * @see com.sasis.dao.ConsumeProductDao#getConsumeProduct(Integer id)
     */
    public ConsumeProduct getConsumeProduct(Integer id) {
        ConsumeProduct consumeProduct = (ConsumeProduct) getSqlMapClientTemplate().queryForObject("getConsumeProduct", id);

        if (consumeProduct == null) {
            throw new ObjectRetrievalFailureException(ConsumeProduct.class, id);
        }

        return consumeProduct;
    }

    /**
     * @see com.sasis.dao.ConsumeProductDao#saveConsumeProduct(ConsumeProduct consumeProduct)
     */    
    public void saveConsumeProduct(final ConsumeProduct consumeProduct) {
        Integer id = consumeProduct.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addConsumeProduct", consumeProduct);
        } else {
            getSqlMapClientTemplate().update("updateConsumeProduct", consumeProduct);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(ConsumeProduct.class, id);
        }
    }

    /**
     * @see com.sasis.dao.ConsumeProductDao#removeConsumeProduct(Integer id)
     */
    public void removeConsumeProduct(ConsumeProduct consumeProduct) {
        getSqlMapClientTemplate().update("deleteConsumeProduct",  consumeProduct);
    }
}
